package com.xbongbong.paas.controller;

/**
 * @author youli.chen
 * @version v1.0
 * @description: TODO
 * @date 2019/2/16 21:04
 * @since v1.0
 */

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.CallAccountRegisterDTO;
import com.xbongbong.paas.pojo.dto.CallCenterAdminLoginDTO;
import com.xbongbong.paas.pojo.dto.CallEnableAudioTranslateDTO;
import com.xbongbong.paas.pojo.dto.CallGetDepUserListDTO;
import com.xbongbong.paas.pojo.dto.CallOrgRegisterDTO;
import com.xbongbong.paas.pojo.dto.CallOrganizationStatusDTO;
import com.xbongbong.paas.pojo.dto.CallStatementListDTO;
import com.xbongbong.paas.pojo.vo.CallAccountRegisterVO;
import com.xbongbong.paas.pojo.vo.CallCenterAdminLoginVO;
import com.xbongbong.paas.pojo.vo.CallEnableAudioTranslateVO;
import com.xbongbong.paas.pojo.vo.CallGetDepUserListVO;
import com.xbongbong.paas.pojo.vo.CallOrgRegisterVO;
import com.xbongbong.paas.pojo.vo.CallOrganizationStatusVO;
import com.xbongbong.paas.pojo.vo.CallStatementListVO;
import com.xbongbong.paas.service.CallCenterAdminService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 话务中心 管理层 API 控制器
 * @date 2019/2/12 14:34
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/callCenter/admin")
public class CallCenterAdminController {

    @Resource
    private CallCenterAdminService callCenterAdminService;

    /**
     * 管理员CFG一键登录获取token
     * @return 管理员CFG一键登录获取token
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String login(@RequestBody @Valid CallCenterAdminLoginDTO callCenterAdminLoginDTO, BindingResult br) {
        XbbResponse<CallCenterAdminLoginVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAdminService.login(callCenterAdminLoginDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 企业注册
     * @return 企业注册
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/org/register", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String organizationRegister(@RequestBody @Valid CallOrgRegisterDTO callOrgRegisterDTO, BindingResult br) {
        XbbResponse<CallOrgRegisterVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAdminService.organizationRegister(callOrgRegisterDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 员工账号注册
     * @return 员工账号注册
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/account/register", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String accountRegister(@RequestBody @Valid CallAccountRegisterDTO callAccountRegisterDTO, BindingResult br) {
        XbbResponse<CallAccountRegisterVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAdminService.accountRegister(callAccountRegisterDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取企业的坐席数量、通话时间等消费信息
     * @return 获取企业的坐席数量、通话时间等消费信息
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/org/status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String organizationStatus(@RequestBody @Valid CallOrganizationStatusDTO callOrganizationStatusDTO, BindingResult br) {
        XbbResponse<CallOrganizationStatusVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAdminService.organizationStatus(callOrganizationStatusDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取部门的员工列表
     * @return 获取部门的员工列表
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/dep/userList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDepUserList(@RequestBody @Valid CallGetDepUserListDTO callGetDepUserListDTO, BindingResult br) {
        XbbResponse<CallGetDepUserListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAdminService.getDepUserList(callGetDepUserListDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 查找坐席费用明细
     * @return 查找坐席费用明细
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/statement/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String statementList(@RequestBody @Valid CallStatementListDTO callStatementListDTO, BindingResult br) {
        XbbResponse<CallStatementListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAdminService.statementList(callStatementListDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 是否允许公司使用话务中心的翻译功能
     * @return 是否允许公司使用话务中心的翻译功能
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/enableAudioTranslate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enableAudioTranslate(@RequestBody @Valid CallEnableAudioTranslateDTO callEnableAudioTranslateDTO, BindingResult br) {
        XbbResponse<CallEnableAudioTranslateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterAdminService.enableAudioTranslate(callEnableAudioTranslateDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
